import './public-path';
import { getRouter } from './router';
import Vue from 'vue'
import App from './App.vue'
import store from './store'

import utils from 'ts-element/packages/utils/utils'
Vue.prototype.$utils = utils
import request from 'ts-element/packages/utils/ajax'
Vue.prototype.$api = request
import { $get, $postJson, $post, $getJson } from 'ts-element/packages/utils/ajax'
Vue.prototype.$get = $get
Vue.prototype.$postJson = $postJson
Vue.prototype.$post = $post
Vue.prototype.$getJson = $getJson
import eventBus from 'ts-element/packages/utils/eventBus'
Vue.prototype.$eventBus = eventBus
import mModal from 'ts-element/packages/utils/confirm'
Vue.prototype.$mModal = mModal


import 'ts-element/packages/utils/btnPermission' // 导入按钮权限js
import 'ts-element/packages/utils/preventReClick'
import tsLibTest from 'ts-element';
Vue.use(tsLibTest);

import 'ts-element/lib/style/index.css'
// import 'ts-design/lib/index/style.css'
import '@/assets/font/iconfont.css'
// import VXETable from 'vxe-table'
// Vue.use(VXETable)
// import 'vxe-table/lib/style.css'
import VueCookies from '@/utils/cookie'
window.$cookies = VueCookies



window.PREVIEW_PATH = '/bi/biPreview_'; //设置预览路径
window.LoginUrl = 'http://192.168.18.228:9999' //登录返回地址

import 'core-js/stable'
import 'regenerator-runtime/runtime'
Vue.config.productionTip = false
import 'xe-utils'

import 'element-ui/lib/theme-chalk/index.css'
Vue.config.productionTip = false;
import 'mutationobserver-shim'

import component from '@/components/index.js'
Vue.use(component)

import cssVars from 'css-vars-ponyfill' // 转义css，兼容var(xxxx)
cssVars({
    watch: true
})
let instance = null;

import Codemirror from 'vue-codemirror';
import 'codemirror/lib/codemirror.css';
Vue.use(Codemirror);
import { Message, Confirm } from 'element-ui'
Vue.prototype.$message = Message
Vue.prototype.$confirm = Confirm

// import { setMessage } from 'ts-element/packages/utils/ajax';
// setMessage(Vue.prototype.$message);

function render(props = {}, qkRouters) {
    const router = getRouter(props, qkRouters);
    instance = new Vue({
        router,
        store,
        render: h => h(App)
    }).$mount(document.getElementById(process.env.VUE_APP_CONTAINER));
}

if (!window.__POWERED_BY_QIANKUN__) {
    require('babel-polyfill')
    render();
}
//测试全局变量污染
// console.log('window.a', window.a)
//更改主题色
function setThemeColor(color) {
    //更换state theme
    store.commit('setTheme', color)
}
export async function bootstrap(props) {
    console.warn('pdc-web bootstraped');
}
export async function mount(props) {
    console.warn('pdc-web mounted');
    global.appLoading = true
    let loading = document.getElementById('loading')
    loading ? loading.style.display = 'none' : ''
    // Vue.prototype.qiankunParentNode = document.getElementById(
    //     process.env.VUE_APP_CONTAINER
    // ).parentNode;
    Object.keys(props.fn).forEach(method => {
        Vue.prototype[`$${method}`] = props.fn[method];
    });
    window.$goLogin = props.fn.goLogin // 注册返回到登录
    global.userInfo = props.data.userInfo
    window.$getCurSysCode = props.fn.getCurSysCode
    if (!localStorage.getItem('token')) {
        localStorage.setItem('token', global.userInfo.token)
        localStorage.setItem('THPMSCookie', global.userInfo.token)
    }
    let lineRoutes = props.data.lineRoutes
    let qkRouters = []
    for (let v of lineRoutes) { // 遍历路由
        let url = v.alink ? v.alink.split('?')[0] : ''
        if (url !== '#' && url !== '/' && url.indexOf(props.data.baseRoute) == 0 && url.indexOf('editor/bi/biEditor') == -1) {
            url = '/' + url.split(props.data.baseRoute)[1]
            let _dir = url
            if (url.indexOf('_') > -1) {
                _dir = url.split('_')[0]
            }
            const d = {
                name: v.id,
                path: url,
                // component: resolve => require(['@/views' + _dir], resolve),
                component: null,
                props: true,
                meta: {
                    title: v.menuname, //页面标题
                    canMultipleOpen: true //支持根据参数不同多开不同页签
                }
            }

            if(_dir === '/') {
                d.component = () => import('@/views/helloword.vue')
            } else {
                d.component = () => import(`@/views${_dir}.vue`)
            }

            if (v.redirect !== undefined) {
                d.redirect = v.redirect
            }
            qkRouters.push(d)
        }
    }
    global.appLoading = false
    //监听主产品下发用户信息
    props.onGlobalStateChange((state, prevState) => {
        if (state.action == 'changeTheme') {
            setThemeColor(state.color)
        }
    });
    // 设置通讯
    Vue.prototype.$onGlobalStateChange = props.onGlobalStateChange;
    Vue.prototype.$setGlobalState = props.setGlobalState;

    render(props, qkRouters);
    if (props.fn.getTheme) {
        let themeColor = props.fn.getTheme()
        if (themeColor) {
            setThemeColor(themeColor)
        }
    }
}
// start({ singular: false });

export async function unmount() {
    // ElementUi.Select.directives.Clickoutside._offMouse(); //卸载ui框架的鼠标按下和松开事件
    instance.$destroy();
}
export default instance
